﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule.MasterData;


public partial class System_Domain_List : PageBase
{
    DomainManager dm;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDomain();
        }

    }

    private void BindDomain()
    {
        dm = new DomainManager();
        GridPager1.DataSource = dm.GetDomainsObject();
        GridPager1.GridView = GridView1;
        GridPager1.BindData();
        GridPager1.Visible = GridView1.Rows.Count > 0;
    }

    /// <summary>
    /// 保存方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            DOMAINS domain = null;
            DomainManager manager = new DomainManager();
           
            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                domain = new DOMAINS();
            }
            else
            {
                domain = manager.Load(int.Parse(GridView1.SelectedValue.ToString()));
            }
            TextBox txtBox = DetailsView1.FindControl("txtDomainID") as TextBox;
            domain.CODE = txtBox.Text;
            txtBox = DetailsView1.FindControl("txtSysName") as TextBox;
            domain.SYSNAME = txtBox.Text;

            txtBox = DetailsView1.FindControl("txtSYSTEM_ADDITIONAL_INFO1") as TextBox;
            domain.SYSTEMADDITIONALINFO1 = txtBox.Text;
            txtBox = DetailsView1.FindControl("txtSYSTEM_ADDITIONAL_INFO2") as TextBox;
            domain.SYSTEMADDITIONALINFO2 = txtBox.Text;
            txtBox = DetailsView1.FindControl("txtSYSTEM_ADDITIONAL_INFO3") as TextBox;
            domain.SYSTEMADDITIONALINFO3 = txtBox.Text;
            txtBox = DetailsView1.FindControl("txtSYSTEM_ADDITIONAL_INFO4") as TextBox;
            domain.SYSTEMADDITIONALINFO4 = txtBox.Text;

            CheckBox cbxValid = DetailsView1.FindControl("chkValid") as CheckBox;
            domain.ACTIVE = cbxValid.Checked;
            domain.LAST_UPDATER = MyStateManager.UserID.ToString();//取到当前登陆用户名
            domain.LAST_UPDATE_TIME = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTimeLong(DateTime.Now);

            domain.ParentDomainID = 0;

            if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
            {
                manager.Save(domain);
            }
            else
            {
                int i = manager.CrateDomain(domain, MyStateManager.UserID.ToString());
                if (i == 0)
                {
                    ShowMessage(UpdatePanel1, "创建成功!");

                }
                else if (i == -1)
                {
                    ShowMessage(UpdatePanel1, "域代码已存在!");
                    return;
                }
                else if (i == -2)
                {
                    ShowMessage(UpdatePanel1, "域名称已存在!");
                    return;
                }
            }

            ScriptManager.RegisterStartupScript(btnSave, this.GetType(), "js", "$('#dd').dialog('close');", true);

            BindDomain();
        }
        catch (Exception ex)
        {
            ShowMessage(UpdatePanel1, ex.Message);
        }

    }

    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        //CheckBox chk = DetailsView1.FindControl("chkValid") as CheckBox;

        //TextBox tb_id = (DetailsView1.FindControl("txtDomainID") as TextBox);

        //TextBox tb_name = (DetailsView1.FindControl("txtSysName") as TextBox);
    }

    private void SetStatus()
    {
        //editArea.Visible = true;
        //editArea.Style.Add("width", "60%");
        //back.Attributes.Add("class", "back");
    }

    //新增
    protected void Button1_Click(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
        SetStatus();
    }

    /// <summary>
    /// 复制数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void but_Copy_Click(object sender, EventArgs e)
    {
        string lb1 = ListBox1.SelectedValue;
        string lb2 = ListBox2.SelectedValue;

        if (string.IsNullOrEmpty(lb1) || string.IsNullOrEmpty(lb2))
        {
            ShowMessage(UpdatePanel3, "请选择源数据及目标项!");
        }
        else
        {
            dm = new DomainManager();
            if (dm.CrateDomain(int.Parse(lb1), int.Parse(lb2), MyStateManager.UserID.ToString()) > -1)
            {
                ShowMessage(UpdatePanel3, "复制数据成功!");
                ScriptManager.RegisterStartupScript(UpdatePanel3, this.GetType(), "js1", "$('#Div1').dialog('close');", true);
            }
            else
            {
                ShowMessage(UpdatePanel3, "复制数据失败!");
            }
        }
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            DetailsView1.ChangeMode(DetailsViewMode.Edit);
            DetailsView1.DataBind();

            (DetailsView1.FindControl("txtDomainID") as TextBox).Enabled = false;
        }
        catch (Exception ex)
        {
            ShowMessage(UpdatePanel1, ex.Message);
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindDomain();
    }

    protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
}




